const Crawler = require('crawler');
const axios = require('axios');
const helper = require('./crawler/helper');
const golbal = require('./crawler/global');
const randomUseragent = require('random-useragent');
// 爬虫测试工具
const site = {
    url: 'http://www.zhzgj.gov.cn/zwfw/ywgs/ghgsgg/ydlgs_50449/',
    siteName: '测试站点',
    origin: '',
    channelId: 0,
    key: '',
    type: 'zhgov',
    weixinName: 'meilidoumen',
    weiboId: 2952681475,
    // 
    wxid: 'oIWsFt3QgDARVGIlWUkfygvqV0X8',
    method: 'GET',
    contentSelector: '.article_con ul li',
    createTime: Date.now,
    state: 1
}

const keyword = {
    name: '测试关键字',
    words: ['富山'],
    emailIds: [],
    state: 1,
    createTime: Date.now
}



let crawler = new Crawler();
let options = getCrawlerOptions(site, keyword);
crawler.queue(options);



function getCrawlerOptions(site, keyword) {
    let options = {
        method: site.method,
        uri: site.url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Origin': site.origin,
            'User-Agent': randomUseragent.getRandom()
        },
        callback: callbackFactory(site, keyword)
    }
    if (site.method.toUpperCase() === 'POST') {
        options.body = getPostBody(site, keyword);
    }
    if (site.type === 'weixin') {
        options.uri = `http://weixin.sogou.com/weixin?type=1&query=${encodeURI(site.weixinName)}`
            // options.referer = 'http://weixin.sogou.com/weixin?type=2';
    }
    if (site.type === 'weibo') {
        options.uri = `https://m.weibo.cn/profile/info?uid=` + site.weiboId;
    }
    return options;
}

function getPostBody(site, keyword) {
    let body = site.key + '=' + keyword.words.join(' ');
    if (site.channelId) {
        body = body + '&channelid=' + site.channelId;
    }
    return body;
}


function callbackFactory(site, keyword) {
    let callback;
    switch (site.type) {
        case 'zhgov':
            callback = function(err, res, done) {
                if (err) {
                    return global.errorHandler(err);
                }
                let $ = res.$;
                let $contents = $(site.contentSelector);
                if ($contents.length) {
                    let contents = helper.parsingGovContents($contents, $, site.url);
                    contents = helper.filterContent(contents, keyword.words);
                    console.log(contents.map(item => item.title));
                }
                done();
            }
            break;
        case 'weibo':
            callback = function(err, res, done) {
                if (err) {
                    return global.errorHandler(err);
                }
                let body = JSON.parse(res.body);
                let contents = body.data.statuses.map(item => {
                    return {
                        keywordId: keyword.id,
                        siteId: site.id,
                        date: item.created_at,
                        weibo: item,
                        title: helper.removeHTML(item.text),
                        link: 'https://m.weibo.cn/profile/' + site.weiboId,
                        keywords: keyword.words.join(' ')
                    }
                });

                contents = helper.filterContent(contents, keyword.words, 'weibo');
                console.log(contents.map(item => item.title));

                done();
            }
            break;
        case 'weixin':
            callback = function(err, res, done) {
                let $ = res.$;

                let $weixinAccounts = $('.news-list2 li');
                if ($weixinAccounts.length) {
                    let $target = $weixinAccounts.eq(0);
                    let url = $target.find('[uigs="account_name_0"]').attr('href');
                    axios.get(url)
                        .then(res => {
                            let data = res.data;
                            let regexp = /var\smsgList\s=\s(\{.+\});\n/
                            let result = regexp.exec(data);
                            if (!result) {
                                console.log('请输入验证码')
                                return;
                            }
                            let weixinData = JSON.parse(result[1]);

                            let contents = helper.parsingWeixinContents(weixinData.list);
                            contents = helper.filterContent(contents, keyword.words, 'weixin');
                            console.log(contents.map(item => item.title));
                        })
                }
                // let $contents = $('.news-list li .txt-box');
                // if ($contents.length) {
                //     let contents = helper.parsingWeixinContents($contents, $);
                //     for (const content of contents) {
                //         content.siteId = site.id;
                //         content.keywordId = keyword.id;
                //         content.keywords = keyword.words.join(' ');
                //     }
                //     console.log(contents.map(item => item.title));
                // }
                done();
            }
            break;
        default:
            callback = function(err, res, done) { done(); };
            break;
    }
    return callback;
}